<?php

class TagsAction extends PublicAction
{
    function index()
    {
        $map['lang'] = LANG_ID;
        $count = M('Tags')->where($map)->count('id');
        if ($count) {
            import("@.ORG.Page");

            $listRows = I('listRows', 15, 'intval');
            $p = new Page($count, $listRows);

            $list = M('tags')->where($map)->limit($p->firstRow . ',' . $p->listRows)->select();

            $this->assign('list', $list);
            $this->assign('page', $p->show());
        }
        $this->display();
    }

    /**
     * 更新
     *
     */
    function edit()
    {
        if (IS_POST) {
            $model = D('Tags');

            if (false === $model->create()) {
                $this->error($model->getError());
            }

            if (false !== $model->save()) {
                $this->success(L('edit_ok'));
            } else {
                $this->success(L('edit_error') . ': ' . $model->getDbError());
            }
        } else {
            $model = M('Tags');
            $pk = ucfirst($model->getPk());
            $id = $_REQUEST[$model->getPk()];

            if (empty($id))
                $this->error(L('do_empty'));

            $do = 'getBy' . $pk;
            $vo = $model->$do($id);

            $this->assign('vo', $vo);

            $this->display();
        }
    }

    public function rebuild()
    {
        $map['lang'] = array('eq', LANG_ID);
        $modelList = M('Model')->where('id>1')->select();

        foreach ($modelList as $model) {

            $dataList = M($model['tablename'])->where($map)->select();
            foreach ($dataList as $item) {
                if ($item['keywords']) {

                    $keywordsarr = explode(',', $item['keywords']);
                    $i = 0;
                    $tagsdata_db = M('Tags_data');
                    $tagsdata_db->where("id=" . $item['id'])->delete();

                    foreach ((array)$keywordsarr as $tagname) {
                        $cat = $this->Categorys[$item['catid']];
                        if ($tagname) {
                            $tagidarr = $tagdatas = $where = array();
                            $where['name'] = array('eq', $tagname);
                            $where['modelid'] = array('eq', $cat['modelid']);
                            $tag = M('Tags')->where($where)->field('id')->find();
                            $tagidarr['id'] = $item['id'];

                            if ($tag['id']) {
                                $num = $tagsdata_db->where("tagid=" . $tag['id'])->count();
                                $tagdatas['num'] = $num + 1;
                                M('Tags')->where("id=" . $tag['id'])->save($tagdatas);
                                $tagidarr['tagid'] = $tag['id'];
                            } else {
                                $tagdatas['modelid'] = $cat['modelid'];
                                $tagdatas['name'] = $tagname;
                                $tagdatas['slug'] = Pinyin($tagname);
                                $tagdatas['num'] = 1;
                                $tagdatas['lang'] = LANG_ID;
                                $tagdatas['model'] = $cat['model'];
                                $tagidarr['tagid'] = M('Tags')->add($tagdatas);
                            }
                            $i++;
                            $tagsdata_db->add($tagidarr);
                        }
                    }
                }
            }
        }

        $this->success('更新成功！');
    }
}